<?php
// +----------------------------------------------------------------------
// |迈远短信接口
// +----------------------------------------------------------------------
// |Data:2015-12-07 16:42
// +----------------------------------------------------------------------
// |Author: lvzhixiong <870923001@qq.com>
// +----------------------------------------------------------------------
namespace  Common\Org\Sms\MaiYuan;
class MainYuan{
    public $_baseUrl = "http://sms.pro-group.com.cn:8888/";
    // gbk
    // $baseUrl = "http://115.29.170.160:8888/smsgbk.aspx ";
    //public $_enterpriseUrl = "http://localhost/yuecheng/rest/simple";
    public $_userid = '419'; // 企业id
    public $_account = 'hy-szht'; // 发送用户帐号
    public $_password = 'shenhaitou2016'; // 发送帐号密码

    /**
     * 上行报告接口
     *
     * @return unknown
     */
    public function callApiquery() {
        $url = $this->_baseUrl . 'callApi.aspx?action=query';
        $action = array (
            'userid' => $this->_userid, // 企业id
            'account' => $this->_account, // 发送用户帐号
            'password' => $this->_password
        ); // 发送帐号密码

        foreach ( $action as $k => $v ) {
            $url = $url . '&' . $k . '=' . $v;
        }
        $return_content = $this->http_post_data ( $url, array () );
        return $return_content;
    }
    /**
     * 状态报告接口
     *
     * @return unknown
     */
    public function query() {
        $url = $this->_baseUrl . 'statusApi.aspx?action=query';
        $action = array (
            'userid' => $this->_userid, // 企业id
            'account' => $this->_account, // 发送用户帐号
            'password' => $this->_password
        ); // 发送帐号密码

        foreach ( $action as $k => $v ) {
            $url = $url . '&' . $k . '=' . $v;
        }
        $return_content = $this->http_post_data ( $url, array () );
        return $return_content;
    }
    /**
     * 非法关键词查询
     *
     * @return unknown
     */
    public function checkkeyword() {
        $url = $this->_baseUrl . 'sms.aspx?action=checkkeyword';
        $action = array (
            'userid' => $this->_userid, // 企业id
            'account' => $this->_account, // 发送用户帐号
            'password' => $this->_password ,
            'content' => '测试'
        ); // 发送帐号密码

        foreach ( $action as $k => $v ) {
            $url = $url . '&' . $k . '=' . $v;
        }
        $return_content = $this->http_post_data ( $url, array () );
        return $return_content;
    }

    /**
     * 查询余额
     *
     * @return unknown
     */
    public function overage() {
        $url = $this->_baseUrl . 'sms.aspx?action=overage';
        $action = array (
            'userid' => $this->_userid, // 企业id
            'account' => $this->_account, // 发送用户帐号
            'password' => $this->_password
        ) // 发送内容
        ; // 发送帐号密码

        foreach ( $action as $k => $v ) {
            $url = $url . '&' . $k . '=' . $v;
        }
        $return_content = $this->http_post_data ( $url, array () );
        return $return_content;
    }
    /**
     * 发送消息
     *
     * @return unknown
     */
    public function sendSms($array,$content) {
        if(!is_array($array) || empty($content)) return false;
        $total = count($array);
        $mobile = implode(',',$array);
        $url = $this->_baseUrl . 'sms.aspx?action=send';
        $action = array (
            'userid' => $this->_userid, // 企业id
            'account' => $this->_account, // 发送用户帐号
            'password' => $this->_password, // 发送帐号密码
            'mobile' => $mobile, // 全部被叫号码
            'content' => $content, // 发送内容
            'sendTime' => '', // 定时发送时间
            'checkcontent' => '1', // 是否检查内容包含非法关键字
            'taskName' => '任务名称', // 任务名称
            'countnumber' => $total, // 号码总数量
            'mobilenumber' => $total, // 手机号码数量
            'telephonenumber' => '0'
        ); // 小灵通或座机号码数

        foreach ( $action as $k => $v ) {
            $url = $url . '&' . $k . '=' . $v;
        }

        $return_content = $this->http_post_data ( $url, array () );
        return $return_content;
    }

    /**
     * 发送post数据
     *
     * @param
     *        	$url
     * @param
     *        	$data_string
     */
    public function http_post_data($url, $data_string) {
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data_string );
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
            'Content-Type: text/html; charset=utf-8'
        ) );
        ob_start ();
        curl_exec ( $ch );
        $return_content = ob_get_contents ();
        ob_end_clean ();

        $return_code = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );

        chmod ( dirname ( __FILE__ ), 0777 );
        // a模式就是一种追加模式，如果是w模式则会删除之前的内容再添加
        $file = fopen ( 'sms_log.txt', 'a+' );
        // 获取需要写入的内容
        // fwrite($data, $file);
        fwrite ( $file, $return_content . '<br>' );
        // 关闭b.php文件
        fclose ( $file );
        // 销毁文件资源句柄变量
        unset ( $file );


        return $return_content;
    }
}
?>